using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera_Ctrl : Singleton_Base<Camera_Ctrl>
{
    public override bool isDontDestroy => false;
    public bool isAcitve=true;
    public Transform target;

    public Transform sourcePoint;

    public float heightInterval = 1;

    public Vector3 minLimitPos;
    public Vector3 maxLimitPos;
    public Vector3 moveVelocity;
    public float smoothTime = 0.1f;
    private void OnDrawGizmosSelected()
    {
        Vector3 sourcePos = sourcePoint == null ? Vector3.zero : sourcePoint.transform.position;
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(sourcePos + new Vector3(minLimitPos.x, minLimitPos.y), sourcePos + new Vector3(maxLimitPos.x, minLimitPos.y));
        Gizmos.DrawLine(sourcePos + new Vector3(minLimitPos.x, minLimitPos.y), sourcePos + new Vector3(minLimitPos.x, maxLimitPos.y));

        Gizmos.DrawLine(sourcePos + new Vector3(maxLimitPos.x, maxLimitPos.y), sourcePos + new Vector3(maxLimitPos.x, minLimitPos.y));
        Gizmos.DrawLine(sourcePos + new Vector3(maxLimitPos.x, maxLimitPos.y), sourcePos + new Vector3(minLimitPos.x, maxLimitPos.y));
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void LateUpdate()
    {
        if (!isAcitve)
        {
            return;
        }
        if (target)
        {
            Vector3 targetPos = target.transform.position ;
            targetPos = new Vector3(Mathf.Clamp(targetPos.x, minLimitPos.x, maxLimitPos.x), Mathf.Clamp(targetPos.y, minLimitPos.y, maxLimitPos.y));
            targetPos.y = heightInterval>0? Mathf.RoundToInt(targetPos.y / heightInterval) * heightInterval: targetPos.y;
            targetPos.z = -10;
            this.transform.position = Vector3.SmoothDamp(this.transform.position, targetPos,ref moveVelocity, smoothTime);
        }
    }
    public void ResetPos()
    {
        Vector3 targetPos = target.transform.position;
        moveVelocity = Vector3.zero;
        targetPos = new Vector3(Mathf.Clamp(targetPos.x, minLimitPos.x, maxLimitPos.x), Mathf.Clamp(targetPos.y, minLimitPos.y, maxLimitPos.y));
        targetPos.y = heightInterval > 0 ? Mathf.RoundToInt(targetPos.y / heightInterval) * heightInterval : targetPos.y;
        targetPos.z = -10;
        this.transform.position = targetPos;
    }
}
